package com.meixiang.fragment.service;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.homes.service.MenuDetailsActivity;
import com.meixiang.adapter.service.MenuClassAdapter;
import com.meixiang.adapter.service.SubjectsAdapter;
import com.meixiang.entity.services.ClassList;
import com.meixiang.entity.services.ClassOneBean;
import com.meixiang.entity.services.classListBean;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.recyclerview.RecycleViewDivider;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 作者：黑哥 on 2016/7/12 18:42
 * <p/>
 * <p/>
 * 基因检测
 */
public class GeneTestingFragment extends BaseFragment implements OnRefreshListener {


    @Bind(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @Bind(R.id.recycler_item_view)
    RecyclerView mRecyclerItemView;
    @Bind(R.id.refresh)
    SwipeToLoadLayout mRefresh;
    private View view;

    private Activity mActivity;

    private String type = "4";


    private classListBean mBean;

    private List<ClassList> mList = new ArrayList<>();

    private List<ClassOneBean> classList = new ArrayList<>();
    private MenuClassAdapter mAdapter;

    private SubjectsAdapter mSubAdapter;

    public GeneTestingFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_gene_testing, container, false);
        mActivity = getActivity();
        super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(activity,RecycleViewDivider.VERTICAL_LIST));
        LinearLayoutManager itemlinearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerItemView.setLayoutManager(itemlinearLayoutManager);
        mRecyclerItemView.addItemDecoration(new RecycleViewDivider(activity,RecycleViewDivider.VERTICAL_LIST));
        mAdapter = new MenuClassAdapter(mActivity);
        mSubAdapter= new SubjectsAdapter(mActivity);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerItemView.setAdapter(mSubAdapter);
        mAdapter.setOnItemClickListener(new MenuClassAdapter.onItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Log.e("TAG","点击了显示当前的条目"+position);
                classList=mAdapter.getData().get(position).getClassList();
                Log.e("TAG","点击了classList"+classList.size());
                mSubAdapter.clear();
                mSubAdapter.addAll(classList);
                mAdapter.selectedPosition=position;
                mAdapter.notifyDataSetChanged();

            }
        });

        mSubAdapter.setOnItemClickListener(new SubjectsAdapter.onItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Intent intent=new Intent(mActivity,MenuDetailsActivity.class);
                intent.putExtra("title","基因检测");
                intent.putExtra("appoitmentType",type);
                intent.putExtra("gcId",mSubAdapter.getData().get(position).getGcId());
                startActivity(intent);
            }
        });
        mRefresh.setOnRefreshListener(this);
        mRefresh.setLoadMoreEnabled(false);

    }


    @Override
    protected void setData() {
        showLoading();
        getHttpData(true);
    }

    private void getHttpData(final boolean isPull) {
        HttpParams params = new HttpParams();
        params.put("gcId", type);
        HttpUtils.post(Config.SERVICE_SELECT_MENU, params, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                Log.e("TAG", "输出数据==" + result.toString());
                if (isPull) {
                    mAdapter.clear();
                }
                if(result != null) {
                    mBean = AbJsonUtil.fromJson(result.toString(), classListBean.class);
                    mList = mBean.getClassList();

                    if (mList == null || mList.size() == 0) {
                        showNoData("暂无数据");
                    } else {
                        classList = mList.get(0).getClassList();
                        removeStatus();

                        mAdapter.addAll(mList);

                        mSubAdapter.clear();
                        mSubAdapter.addAll(classList);

                    }
                }

            }

            @Override
            public void onError(String code, String message) {
                showNoData(message, "重新加载", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showLoading();
                        getHttpData(true);
                    }
                });
            }

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                super.onAfter(isFromCache, jsonObject, call, response, e);
                if (mRefresh.isRefreshing()){
                    mRefresh.setRefreshing(false);
                }else{
                    mRefresh.setLoadingMore(false);
                }
            }

        });


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @Override
    public void onRefresh() {
        getHttpData(true);
    }
}
